package org.example.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.domain.po.ErpInOutItem;
import org.example.domain.po.ErpUser;
import org.example.domain.po.UpdateStatusRequest;
import org.example.service.ErpInOutItemService;
import org.example.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/basic/in_out_item")
public class ErpInOutItemController {

    @Autowired
    ErpInOutItemService erpInOutItemService;

    @GetMapping("/find")
    public Result<Page<ErpInOutItem>> findByPage(
            @RequestParam(defaultValue = "1") Integer current,
            @RequestParam(defaultValue = "5") Integer size,
            @RequestAttribute("user") ErpUser user,
            String itemName,
            String type,
            String remark
    ){
        Page<ErpInOutItem> page = new Page<>(current, size);
        page = erpInOutItemService.findByPage(page, itemName, type, remark, user.getTenantId());
        return Result.success(page);
    }

    @PostMapping("/insert")
    public Result insert(@RequestAttribute("user") ErpUser user, @RequestBody ErpInOutItem erpInOutItem){
        erpInOutItem.setTenantId(Math.toIntExact(user.getTenantId()));
        return erpInOutItemService.save(erpInOutItem) ? Result.success("新增成功！") : Result.success("新增失败！");
    }

    @PutMapping("/update")
    public Result update(@RequestBody ErpInOutItem erpInOutItem){
        return erpInOutItemService.updateById(erpInOutItem) ? Result.success("编辑成功！") : Result.success("编辑失败！");
    }

    @DeleteMapping("/{itemId}")
    public Result<String> remove(@PathVariable("itemId") Integer itemId){
        return erpInOutItemService.removeById(itemId) ? Result.success("删除成功！") : Result.success("删除失败！");
    }

    /**
     * 批量删除
     * @param erpInOutItems
     * @return
     */
    @PostMapping("/batch")
    public Result removeAll(@RequestBody List<ErpInOutItem> erpInOutItems){
        return erpInOutItemService.removeBatchByIds(erpInOutItems) ? Result.success("操作成功！") : Result.success("操作失败！");
    }

    /**
     * 批量修改
     * @param modify
     * @return
     */
    @PutMapping("/modifyTheState")
    public Result<String> modifyTheState(@RequestBody UpdateStatusRequest modify){
        boolean theState = erpInOutItemService.modifyTheState(modify.getIds(), modify.getStatus());
        return theState ? Result.success("操作成功！") : Result.success("操作失败！");
    }
}
